package com.zhn.evsi.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhn.evsi.client.FitCilent;
import com.zhn.evsi.client.ThirdClient;
import com.zhn.evsi.domain.dto.evsi.EvsiFitDTO;
import com.zhn.evsi.domain.dto.evsi.EvsiMarketStatDTO;
import com.zhn.evsi.domain.dto.evsi.EvsiMarketStatSellDTO;
import com.zhn.evsi.domain.dto.evsi.character.EvsiCharacterPublicInfoDTO;
import com.zhn.evsi.domain.entity.evsi.EvsiFit;
import com.zhn.evsi.domain.entity.evsi.EvsiGraphic;
import com.zhn.evsi.domain.entity.evsi.EvsiIcons;
import com.zhn.evsi.domain.entity.evsi.EvsiInvtypes;
import com.zhn.evsi.interceptor.CharacterInterceptor;
import com.zhn.evsi.mapper.EvsiFitMapper;
import com.zhn.evsi.service.IEvsiFitService;
import com.zhn.evsi.service.IEvsiGraphicService;
import com.zhn.evsi.service.IEvsiIconsService;
import com.zhn.evsi.service.IEvsiInvtypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class EvsiFitServiceImpl extends ServiceImpl<EvsiFitMapper, EvsiFit> implements IEvsiFitService {

    @Autowired
    private FitCilent fitCilent;
    @Autowired
    private ThirdClient thirdClient;
    @Autowired
    private IEvsiIconsService iconsService;
    @Autowired
    private IEvsiGraphicService graphicService;
    @Autowired
    private IEvsiInvtypesService invtypesService;
    @Autowired
    private CharacterInterceptor characterInterceptor;

    /**
     * 舰船配置查询
     * */
    @Override
    public List<EvsiFitDTO> fittings() {
        // 获取当前角色
        EvsiCharacterPublicInfoDTO characters = characterInterceptor.getCharacters(null);
        List<EvsiFitDTO> FitDTOList = new ArrayList<>();
        //API获取数据
        List<EvsiFitDTO> fittings = fitCilent.fittings(characters.getCharacters());
        fittings.forEach(fitting -> {
            List<Integer> typeIds = new ArrayList<>();
            // 获取舰船信息
            EvsiInvtypes invtypeList = invtypesService.getOne(new LambdaQueryWrapper<EvsiInvtypes>().eq(EvsiInvtypes::getTypeId, fitting.getShip_type_id()));
            if (ObjectUtil.isNotNull(invtypeList.getGraphicId())) {
                // TODO
                fitting.setGraphic_id(invtypeList.getGraphicId());
                String folder = graphicService.getOne(new LambdaQueryWrapper<EvsiGraphic>()
                        .eq(EvsiGraphic::getGraphicId, invtypeList.getGraphicId())).getFolder();
                fitting.setGraphic_folder(folder);
            }
            fitting.getItems().forEach(k -> {

                EvsiInvtypes invtypes = invtypesService.getOne(new LambdaQueryWrapper<EvsiInvtypes>().eq(EvsiInvtypes::getTypeId, k.getType_id()));
                k.setType_name(invtypes.getTypeName());
                k.setIcon_id(invtypes.getIconId());
                EvsiIcons icons = iconsService.getOne(new LambdaQueryWrapper<EvsiIcons>().eq(EvsiIcons::getIconId, invtypes.getIconId()));
                if (ObjectUtil.isNotNull(icons)) {
                    k.setIcon_file(icons.getIconFile());
                }
                if (ObjectUtil.isNotNull(invtypes.getGraphicId())) {
                    k.setGraphic_id(invtypes.getGraphicId());
                    String folder = graphicService.getOne(new LambdaQueryWrapper<EvsiGraphic>()
                            .eq(EvsiGraphic::getGraphicId, invtypeList.getGraphicId())).getFolder();
                    k.setGraphic_folder(folder);
                }
                typeIds.add(k.getType_id());
            });
            // 获取当前舰船的typeid，并获取当前装备的typeid。然后  合并.join成string 字符串，帝丹房client导入
            typeIds.add(fitting.getShip_type_id());
            String join = CollUtil.join(typeIds, ",");
            List<JSONObject> marketstat = thirdClient.marketstat(join, "10000002");
            List<Double> sellMinList = new ArrayList<>();
            marketstat.forEach(v -> {
                EvsiMarketStatDTO evsiMarketStatDTO = JSON.toJavaObject(v, EvsiMarketStatDTO.class);
                sellMinList.add(evsiMarketStatDTO.getSell().getMin());
            });
            fitting.setShip_price(sellMinList.stream().reduce(Double::sum).get());
            FitDTOList.add(fitting);
        });
        return FitDTOList;
    }
}
